using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using  server.status_db;
[Authorize]
public class LockController : ODataController
{
    private status_oneContext _db;
    public LockController(status_oneContext context)
    {
        _db = context;

    }
    private bool RightExists(Guid key)
    {
        return _db.SET_LOCK.Any(p => p.ID == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody] SET_LOCK update)
    {
        if (!ModelState.IsValid)
        {
               var message = string.Join(" | ", ModelState.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.Exception.Message));
            return   StatusCode(500, message);
        }
        if (key != update.ID)
        {
            return BadRequest();
        }
        _db.Entry(update).State = EntityState.Modified;
        try
        {
            await _db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!RightExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(update);
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {
        var mc = await _db.SET_LOCK.FindAsync(key);
        if (mc == null)
        {
            return NotFound();
        }
        _db.SET_LOCK.Remove(mc);
        await _db.SaveChangesAsync();
        return NoContent();
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] SET_LOCK model)
    {
       
       if (!ModelState.IsValid)
        {
               var message = string.Join(" | ", ModelState.Values
                .SelectMany(v => v.Errors)
                .Select(e => e.Exception.Message));
            return   StatusCode(500, message);
        }
        _db.SET_LOCK.Add(model);
        await _db.SaveChangesAsync();

        return Created(model);
    }
    [EnableQuery]
    public IActionResult Get()
    {
        return Ok(_db.SET_LOCK);
    }
    //
}